package com.houguanghui.examples.netty.quickstart;

import java.nio.charset.StandardCharsets;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class HelloWordClientHandler extends ChannelInboundHandlerAdapter {
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) {
    // 将接收到的 ByteBuf 转换为字符串
    ByteBuf buffer = (ByteBuf) msg;
    try {
      String message = buffer.toString(StandardCharsets.UTF_8);
      System.out.println("接收到服务器消息: " + message);
    } finally {
      buffer.release(); // 重要：释放缓冲区
    }
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();
    ctx.close();
  }
}
